package handlers

import (
	"github.com/gin-gonic/gin"
	"rk8-core/cache"
	"rk8-core/models"
	"strconv"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var article []models.Article

	redis_key := "list"
	cache.Rdb.Get(cache.Ctx, redis_key)
	models.DB.Order("title DESC").Limit(size).Offset(offset).Find(&article)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": article,
	})
	return

}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var article models.Article
	redis_key := "article_" + id
	cache.Rdb.Get(cache.Ctx, redis_key)
	models.DB.Where("id=?", id).Find(&article)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
	})
	return
}
